package com.yuandian.management.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.management.entity.ConfMpAlarmPeriod;
import com.yuandian.api.management.po.ConfMpAlarmParams;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.api.management.entity.ConfMpAlarm;
import com.yuandian.management.service.ConfMpAlarmService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 中台监控-告警配置表-Controller
 * @author zfsn
 * @date 2023-4-18
 */
@Api(tags = "中台监控-告警配置")
@RestController
@RequestMapping("/mp/alarm")
public class ConfMpAlarmController {

	@Autowired
	private ConfMpAlarmService confMpAlarmService;

	/**
	 * 通过主键查询数据
	 * @param idList 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "主键查询", notes = "通过主键查询告警配置，支持批量查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "主键数组", paramType = "path", required = true, dataType = "String", dataTypeClass = String.class)
	})
	@GetMapping("/queryByIds/{ids}")
	// @PreAuthorize("@pms.hasPermission('conf_mp_alarm_view')")
	public R<List<ConfMpAlarm>> queryByIds(@PathVariable("ids") List<Integer> idList) {
		return R.ok(confMpAlarmService.listByIds(idList));
	}

	/**
	 * 分页查询
	 * @param params 中台监控-告警配置请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "current", value = "当前页", paramType = "body", required = true, dataType = "Long", dataTypeClass = Long.class),
			@ApiImplicitParam(name = "size", value = "每页条数", paramType = "body", required = true, dataType = "Long", dataTypeClass = Long.class),
			@ApiImplicitParam(name = "orders", value = "排序方式", paramType = "body", required = false, dataType = "OrderItem", dataTypeClass = OrderItem.class, allowMultiple = true),
			@ApiImplicitParam(name = "serviceId", value = "所属服务id", paramType = "body", required = false, dataType = "Integer", dataTypeClass = Integer.class),
	})
	@PostMapping("/page")
	// @PreAuthorize("@pms.hasPermission('conf_mp_alarm_view')")
	public R<Page<ConfMpAlarm>> page(@ApiIgnore @RequestBody ConfMpAlarmParams params) {
		return confMpAlarmService.page(params);
	}

	/**
	 * 新增数据
	 * @param params 中台监控-告警配置请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "新增告警", notes = "新增告警")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "serviceId", value = "所属服务id", paramType = "body", required = true, dataType = "Integer", dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "alarmCode", value = "告警指标编码", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "alarmName", value = "告警指标名称", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "levelId", value = "告警等级id", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "threshold", value = "阈值", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "unitId", value = "阈值单位id", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "template", value = "告警模板", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "statisticalGranularity", value = "统计粒度(分钟)", paramType = "body", required = true, dataType = "Integer", dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "isEnable", value = "是否启用", paramType = "body", required = true, dataType = "Integer", dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "periodList", value = "告警周期段", paramType = "body", required = true, dataType = "ConfMpAlarmPeriod", dataTypeClass = ConfMpAlarmPeriod.class),
			@ApiImplicitParam(name = "remarks", value = "描述", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class)
	})
	@PostMapping("/save")
	// @PreAuthorize("@pms.hasPermission('conf_mp_alarm_add')")
	public R<Boolean> save(@ApiIgnore @RequestBody ConfMpAlarmParams params) {
		return confMpAlarmService.saveWrap(params);
	}

	/**
	 * 通过主键更新数据
	 * @param params 中台监控-告警配置请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "主键更新", notes = "通过主键更新告警配置")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "主键", paramType = "body", required = true, dataType = "Integer", dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "serviceId", value = "所属服务id", paramType = "body", required = false, dataType = "Integer", dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "alarmCode", value = "告警指标编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "alarmName", value = "告警指标名称", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "levelId", value = "告警等级id", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "threshold", value = "阈值", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "unitId", value = "阈值单位id", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "template", value = "告警模板", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "statisticalGranularity", value = "统计粒度(分钟)", paramType = "body", required = false, dataType = "Integer", dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "isEnable", value = "是否启用", paramType = "body", required = false, dataType = "Integer", dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "periodList", value = "告警周期段", paramType = "body", required = false, dataType = "ConfMpAlarmPeriod", dataTypeClass = ConfMpAlarmPeriod.class),
			@ApiImplicitParam(name = "remarks", value = "描述", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class)
	})
	@PutMapping("/updateById")
	// @PreAuthorize("@pms.hasPermission('conf_mp_alarm_update')")
	public R<Boolean> updateById(@ApiIgnore @RequestBody ConfMpAlarmParams params) {
		return confMpAlarmService.updateByIdWrap(params);
	}

	/**
	 * 通过主键删除数据
	 * @param idList 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "主键删除", notes = "通过主键删除告警配置，支持批量删除")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "主键数组", paramType = "path", required = true, dataType = "String", dataTypeClass = String.class)
	})
	@DeleteMapping("/removeByIds/{ids}")
	// @PreAuthorize("@pms.hasPermission('conf_mp_alarm_delete')")
	public R<Boolean> removeByIds(@PathVariable("ids") List<Integer> idList) {
		return confMpAlarmService.removeByIdsWrap(idList);
	}

	/**
	 * 通过告警id查询告警时间段
	 * @param alarmId 告警id
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "告警时间段查询", notes = "通过告警id查询告警时间段")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "alarmId", value = "告警id", paramType = "path", required = true, dataType = "Integer", dataTypeClass = Integer.class),
	})
	@GetMapping("/queryPeriodByAlarmId/{alarmId}")
	public R<List<ConfMpAlarmPeriod>> queryPeriodByAlarmId(@PathVariable("alarmId") Integer alarmId) {
		return confMpAlarmService.queryPeriodByAlarmId(alarmId);
	}

	/**
	 * 通过告警id更新告警时间段
	 * @param params 中台监控-告警配置请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "告警时间段更新", notes = "通过告警id更新告警时间段")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "告警id", paramType = "body", required = true, dataType = "Integer", dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "periodList", value = "告警周期段", paramType = "body", required = false, dataType = "ConfMpAlarmPeriod", dataTypeClass = ConfMpAlarmPeriod.class),
	})
	@PutMapping("/updatePeriodByAlarmId")
	public R<Boolean> updatePeriodByAlarmId(@ApiIgnore @RequestBody ConfMpAlarmParams params) {
		return confMpAlarmService.updatePeriodByAlarmId(params);
	}
}